﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.UI.common;
using rock.common;
using rock.DAL;

namespace rock.UI
{
    public partial class FormConfigSql : Form
    {
        public FormConfigSql()
        {
            InitializeComponent();

            this.AcceptButton = this.button1;
            this.CancelButton = this.button2;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.WindowState = FormWindowState.Normal;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = global::rock.Properties.Resources.house;

        }
        string IpAddress;
        string Port;
        string DataBaseName;
        string Username;
        string Password;
        string ConnectionString;
        private void FormConfigSql_Load(object sender, EventArgs e)
        {
            String sqlCon = ConfigUtils.GetConnectionStringsConfig("rock.Properties.Settings.RentalHousingManagerConnectionString");
            String[] data = sqlCon.Split(new char[2] { ';', '=' });

            string[] IpAddressport = data[1].Split(new char[] { ',' });
            IpAddress =IpAddressport[0];
            Port = IpAddressport[1];

            this.DataBaseName = data[3];
            this.Username = data[5];
            this.Password = data[7];
          //  ConnectionString = "Data Source=" + this.IpAddress + ";User id=" + this.Username + ";Password=" + this.Password + "; Initial Catalog=master"; 

            this.textBox1Ip.Text = IpAddress;
            this.textBox1Port.Text = Port;
            this.textBox4DbName.Text = DataBaseName;
            this.textBox2Sa.Text = Username;
            this.textBox3Pwd.Text = Password;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                StringUtils.checkText(this.textBox1Ip.Text, 2, 50, "数据库服务器地址");
                StringUtils.checkIsPositiveInteger(this.textBox1Port.Text,  "登陆数据库名");
                StringUtils.checkText(this.textBox4DbName.Text, 2, 30, "登陆数据库名");

                StringUtils.checkText(this.textBox2Sa.Text, 2, 30, "数据库登陆帐号");
                StringUtils.checkText(this.textBox3Pwd.Text, 2, 30, "数据库登陆密码");
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message, "提示");
                return  ;
            }
            String newName = "rock.Properties.Settings.RentalHousingManagerConnectionString";
            String connectionName = "Data Source=" + this.textBox1Ip.Text + "," + this.textBox1Port.Text + ";Initial Catalog=" + this.textBox4DbName.Text + ";User ID=" + this.textBox2Sa.Text + ";password=" + this.textBox3Pwd.Text + ";Connection Timeout=10";
            String providerName =  "System.Data.SqlClient";
            ConfigUtils.UpdateConnectionStringsConfig(newName, connectionName, providerName);

            DbHelperSQL.connectionString = ConfigUtils.GetConnectionStringsConfig("rock.Properties.Settings.RentalHousingManagerConnectionString");
            try
            {
                oneRefreshParentData();
            }
            catch (Exception)
            {
                
                throw;
            }
            
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //代理
        public delegate void RefreshParentData();
        public RefreshParentData oneRefreshParentData;
    }
}
